package com.blogs.service;

import java.text.SimpleDateFormat;
import java.util.Date;

import com.blogs.entity.AppEnums.IngPublicType;
import com.blogs.entity.SendIngEntry;
import com.cnblogs.R;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class SendIngService extends Service {
	private static volatile int serviceCount = 0;
	private NotificationManager nm;
	private Notification baseNF;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
	}

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		Log.i("startId", startId + "");
		if (intent != null) {
			String sid = intent.getStringExtra("sid");
			final String author = intent.getStringExtra("author");
			final String avatar = intent.getStringExtra("avatar");
			final int position=intent.getIntExtra("position", -1);
			final int ContentId = intent.getIntExtra("ContentId", 0);
			final int ReplyTo = intent.getIntExtra("ReplyTo", 0);
			int ParentCommentId = intent.getIntExtra("ParentCommentId", 0);
			final String Content = intent.getStringExtra("Content");
			serviceCount++;
			setNotification(R.drawable.queue_icon_send, "闪存发送", "闪存发送中");
			new SendIng(sid, ContentId, ReplyTo, ParentCommentId, Content, new SendIng.IMsgCallBack() {

				public void onSuccess(SendIngEntry sce) {
					// TODO Auto-generated method stub
					if (sce.IsSuccess == 1) {
						setNotification(R.drawable.queue_icon_success, "评论发送", "闪存成功");
						SimpleDateFormat timeFormat = new SimpleDateFormat("yyyy-MM-dd$HH:mm:ss");
						Intent intent = new Intent("IngPublicReceiver");
						intent.putExtra("IngPublicType",ContentId==0? IngPublicType.New:IngPublicType.Com);
						intent.putExtra("position", position);
						intent.putExtra("ContentId", ContentId);
						intent.putExtra("Content", Content);
						intent.putExtra("author", author);
						intent.putExtra("avatar", avatar);
						intent.putExtra("author_id", ReplyTo);
						intent.putExtra("comment_time", timeFormat.format(new Date()).replace("$", " "));
						sendBroadcast(intent);// 发送广播
						
						Intent comintent = new Intent("IngComPublicReceiver");
						comintent.putExtra("IngPublicType",ContentId==0? IngPublicType.New:IngPublicType.Com);
						comintent.putExtra("position", position);
						comintent.putExtra("ContentId", ContentId);
						comintent.putExtra("Content", Content);
						comintent.putExtra("author", author);
						comintent.putExtra("avatar", avatar);
						comintent.putExtra("author_id", ReplyTo);
						comintent.putExtra("comment_time", timeFormat.format(new Date()).replace("$", " "));
						sendBroadcast(comintent);// 发送广播
					} else {
						setNotification(R.drawable.queue_icon_miss, "发送失败", "发送失败," + sce.responseText);
					}
					serviceCount--;
					if (serviceCount == 0)
						stopTheServer();
				}

				public void onFailed() {
					// TODO Auto-generated method stub

					setNotification(R.drawable.queue_icon_miss, "闪存发送", "闪存发送失败");
					serviceCount--;
					if (serviceCount == 0)
						stopTheServer();
				}
			}).Send();
		}
		return super.onStartCommand(intent, flags, startId);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	private void setNotification(int icon, String title, String con) {
		baseNF = new Notification(icon, con, System.currentTimeMillis());
		// baseNF=new Notification();
		// baseNF.icon=R.drawable.queue_icon_send;
		// baseNF.tickerText="评论发送中";
		// baseNF.when=System.currentTimeMillis();
		baseNF.flags = Notification.FLAG_AUTO_CANCEL;
		Intent nintent = new Intent();
		nintent.setAction(Intent.ACTION_MAIN);
		nintent.addCategory(Intent.CATEGORY_LAUNCHER);
		nintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		nintent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
		PendingIntent pendIntent = PendingIntent.getActivity(SendIngService.this, 0, nintent, 0);
		baseNF.setLatestEventInfo(SendIngService.this, title, con, pendIntent);
		nm.notify(R.string.app_name, baseNF);
		nm.cancelAll();
	}

	private void stopTheServer() {
		this.stopSelf();
	}
}
